﻿using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Enhance.TypeConvert;
using System;
using System.Text;

namespace CatUtils.Utils.Enhance.Encrypt
{
    /// <summary>
    /// 配置文件加密辅助
    /// </summary>
    public class ConfigurationEncrypt
    {
        /// <summary>
        /// 传入配置信息和保存路径，进行加密后保存。
        /// </summary>
        /// <param name="SettingConfiguration">需要加密的设置信息</param>
        /// <param name="FilePath">配置文件路径</param>
        /// <param name="OpenBase64">是否打开BASE64加密</param>
        /// <param name="ContentReverse">是否反转字母顺序</param>

        /// <returns></returns>
        public static void SaveCofiguration(string SettingConfiguration, string FilePath, bool OpenBase64 = false, bool ContentReverse = false)
        {
            if (OpenBase64)
            {
                SettingConfiguration = new Base64Convert().EnBase64(SettingConfiguration);
            }
            if (ContentReverse)
            {
                byte[] vs = Encoding.UTF8.GetBytes(SettingConfiguration);
                Array.Reverse(vs);
                SettingConfiguration = Encoding.UTF8.GetString(vs, 0, vs.Length);
            }

            FileHelper.WriteText(FilePath, SettingConfiguration);
        }

        /// <summary>
        /// 获取加密的配置文件，取得相关结果
        /// </summary>
        /// <param name="FilePath">配置文件路径</param>
        /// <param name="OpenBase64">是否打开BASE64加密</param>
        /// <param name="ContentReverse">是否反转字母顺序</param>

        public static string GetCofiguration(string FilePath, bool OpenBase64 = false, bool ContentReverse = false)
        {
            string SettingConfiguration = FileHelper.ReadAllText(FilePath);

            if (ContentReverse)
            {
                byte[] vs = Encoding.UTF8.GetBytes(SettingConfiguration);
                Array.Reverse(vs);
                SettingConfiguration = Encoding.UTF8.GetString(vs, 0, vs.Length);
            }
            if (OpenBase64)
            {
                SettingConfiguration = new Base64Convert().DeBase64(SettingConfiguration);
            }

            return SettingConfiguration;
        }
    }
}